<?php
$this->pageTitle = 'Danh sách thành viên';
$this->breadcrumbs = array(
	'Thành viên'=>array('index')
);



//$this->menu=array(
//	array('label'=>'Create Member', 'url'=>array('create')),
//);
//
//Util::registerScript('search', "
//$('.search-button').click(function(){
//	$('.search-form').toggle();
//	return false;
//});
//$('.search-form form').submit(function(){
//	$.fn.yiiGridView.update('member-grid', {
//		data: $(this).serialize()
//	});
//	return false;
//});
//
//");


?>
<div class="heading">
    <h1><img src="<?php echo Util::themeUrl() ?>/images/header.ico" /> <?php echo $this->pageTitle;?></h1>

    <div class="controls">
        <?php
        	echo CHtml::button('Đăng kí từ excel', array('class' => 'controls', 'id' => 'import-member'));
	        echo CHtml::button('Đăng kí', array('class' => 'controls', 'id' => 'create-member'));
	        echo CHtml::button('Xóa', array('class' => 'controls', 'id' => 'delete-member'));
        ?>
    </div>
</div>

<div class="content">

<?php
// echo CHtml::link('Advanced Search','#',array('class'=>'search-button'));
// echo '<div class="search-form" style="display:none">';
// $this->renderPartial('_search',array(
//	'model'=>$model,
// ));
// echo '</div><!-- search-form -->';
?>


<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'member-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
        'cssFile' => Util::themeUrl() . '/css/gridview.css',
        'htmlOptions'=>array('class'=>'grid-view clear'),
        'afterAjaxUpdate'=>'js:function(id,data){$.bind_data()}',
	'columns'=>array(
		array(
            'header' =>  'No.',
            'value' => '$row + Util::getPaggingIndex()',
            'htmlOptions' => array(
                'style' => 'width:25px; text-align:center'
            )
        ),
        array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            'id' => 'chk',
            'value' => '$data->user_id',
        ),
        'courses_id' => array(
            'name' => 'courses_id',
            'filter' => CHtml::listData(Courses::model()->findAll(array("condition" => "status ='1'")), "id", "name"),
            'value' => '$data->codeactive->courses->id',
			'htmlOptions' => array(
                'style' => 'width:80px; text-align:center',
            )
		),
        array(
			'name'	=> 'code',
			'type'	=>'raw',
			'value' => 'Util::markSearch($data , "code", $data->codeactive->code)',
        	'htmlOptions' => array(
                'style' => 'width:80px; text-align:center'
            )
		),
		array(
			'name'	=>'fullname',
			'type'	=>'raw',
			'value' =>'Util::markSearch($data , "fullname", Util::formatFullName($data->fullname))',
			'htmlOptions' => array(
                'style' => 'width:190px;'
            )
		),
		array(
			'name'			=>'sex',
			'type'			=>'raw',
			'value'			=>'$data->sex != null ? Util::itemAlias("Sex", $data->sex) : ""',
			'filter' 		=> Util::itemAlias("Sex"),
      		'htmlOptions' 	=> array(
                'style' => 'width:60px;text-align:center',
            )
		),
		array(
			'name'	=>'email',
			'type'	=>'raw',
			'value'	=> 'CHtml::link(Util::markSearch($data,"email"), "mailto:".$data->email)',
		),
		array(
			'name'	=> 'lastvisit_at',
			'type'	=>'raw',
			'value' => 'Util::formatTime($data->account->lastvisit_at, true, "-", true)',
			'filter' => false,
        	'htmlOptions' => array(
                'style' => 'width:120px; text-align:center'
            )
		),
		/*
		array(
		'name'	=>'birthday',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"birthday")',
		),
		array(
		'name'	=>'birthplace',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"birthplace")',
		),
		array(
		'name'	=>'phone',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"phone")',
		),
		array(
		'name'	=>'cellphone',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"cellphone")',
		),
		array(
		'name'	=>'email',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"email")',
		),
		array(
		'name'	=>'address',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"address")',
		),
		array(
		'name'	=>'contact_address',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"contact_address")',
		),
		array(
		'name'	=>'avatar',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"avatar")',
		),
		array(
		'name'	=>'school_name',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"school_name")',
		),
		array(
		'name'	=>'major',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"major")',
		),
		array(
		'name'	=>'start_year',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"start_year")',
		),
		array(
		'name'	=>'end_year',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"end_year")',
		),
		array(
		'name'	=>'social_position',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"social_position")',
		),
		array(
		'name'	=>'social_activity',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"social_activity")',
		),
		array(
		'name'	=>'courses_id',
		'type'	=>'raw',
		'value'	=>'Util::markSearch($data,"courses_id")',
		),
		*/
		array(
			'class'=>'CButtonColumn',
			'template' => '{view} {update}',
			'htmlOptions' => array(
                'style' => 'width:50px;text-align:center',
            )

		),
	),
)); ?>
</div>

<script  type="text/javascript">
    $(document).ready(function() {
        $. bind_data = function(){
            $(".grid-view a.view").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('member-grid')
                    }
                });
                return false;
            });

            $(".grid-view a.update").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('member-grid')
                    }
                });
                return false;
            });
        }

        // bind data
        $. bind_data();

        $("#create-member").click(function() {
                $.fancybox.open({
                    href : '<?php echo $this->createUrl('create', array('ajax' => true)) ?>',
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('member-grid')
                    }
                });
                return false;
            });

        $('#delete-member').live('click', function(){
            var count=0;
            var params = 'YII_CSRF_TOKEN=<?php echo  Yii::app()->request->csrfToken; ?>';
            $("input[name='chk[]']").each(function(){
            if($(this).is(":checked")){
                count++;
                params += '&chk[]=' + $(this).val();
            }
            });
            if(count >= 1 ){
            if(!confirm("Xóa dữ liệu được chọn?"))
                return false;
            }else{
                alert("Chưa chọn dữ liệu muốn xóa.");
                return false;
            }

            $.ajax({
                    type: "POST",
                    url: '<?php echo $this->createUrl('delete', array('ajax' => true)) ?>',
                    data: params,
                    success: function(data) {
                    	alert('Dữ liệu xóa thành công.');
                        $.fn.yiiGridView.update('member-grid');
                    } //success
                });//ajax
                return false;
        });

        $("#import-member").click(function() {
            $.fancybox.open({
                href : '<?php echo $this->createUrl('importMember', array('ajax' => true)) ?>',
                type : 'iframe',
                afterClose: function(){
                    $.fn.yiiGridView.update('member-grid')
                }
            });
            return false;
        });
    });
</script>